package jp.sourceforge.akjrcp.text_douga.item;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.ItemData;

public abstract class AbstractItem implements ItemData{
	public static Log log=LogFactory.getLog(AbstractItem.class);
	
	
	protected int align;
	protected int valign;
	protected double startTime;
	protected double duration = 1.0;
	protected  double fadeInTime = 0.2;
	protected  double fadeOutTime = 0.2;
	protected  float transparent = 1.0f;

	protected int x;
	protected int y;
	
	protected int width;
	protected int height;
	
	protected int z;
	
	protected int marginLeft=16;
	protected int marginRight=16;
	protected int marginTop=16;
	protected int marginBottom=16;
	
	protected boolean keepAspect=true;
	
	public void setBounds(int x,int y,int width,int height){
		setX(x);
		setY(y);
		setWidth(width);
		setHeight(height);
	}
	public String  toString(){
		return "startTime="+startTime+" duration="+duration+" fadeInTime="+fadeInTime+" fadeOutTime="+fadeOutTime+" transparent="+transparent+" x="+x
				+" y="+y+" width="+width+" height="+height+" z="+z+" align="+align+ " valign="+valign+" keepAspect="+keepAspect;
	}
	public int getZ() {
		// small is firster
		return z;
	}

	public float getFrameTransparent(double time) {
		if(duration==DougaSystem.FOREEVER){
			return 1;//draw alltime
		}
		double drawTransparent=transparent;
		double t=time-startTime;
		if(t<=0 || t>=duration){//luckey
			if(t<0 || t>duration)
				log.warn("invalid t:"+t);
			if(fadeInTime==0 &&t<=0){
				return transparent;
			}else if(fadeOutTime==0 && t>=duration){
				return transparent;
			}
			return 0.03f;//if don't draw useless 
		}
		if(t>0 && t<fadeInTime){//fade in
			drawTransparent=t/fadeInTime*transparent;
			if(drawTransparent<0 || drawTransparent>1){
				log.warn("invalid fadeIn:"+"time="+t+","+drawTransparent);
			}
		}else if(t>duration-fadeOutTime){//fade out
			drawTransparent=(duration-t)/fadeOutTime*transparent;
			if(drawTransparent<0 || drawTransparent>1){
				log.warn("invalid fadeOut:"+"time="+t+","+drawTransparent);
			}
		}
		
		return (float)drawTransparent;
	}

	public float getTransparent() {
		return transparent;
	}

	public void setTransparent(float transparent) {
		this.transparent = transparent;
	}

	public int getAlign() {
		return align;
	}

	public void setAlign(int align) {
		this.align = align;
	}

	public double getDuration() {
		return duration;
	}

	public void setDuration(double duration) {
		this.duration = duration;
	}


	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public double getStartTime() {
		return startTime;
	}

	public void setStartTime(double startTime) {
		this.startTime = startTime;
	}

	public int getValign() {
		return valign;
	}

	public void setValign(int valign) {
		this.valign = valign;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setZ(int z) {
		this.z = z;
	}

	public double getFadeInTime() {
		return fadeInTime;
	}

	public void setFadeInTime(double fadeInTime) {
		this.fadeInTime = fadeInTime;
	}

	public double getFadeOutTime() {
		return fadeOutTime;
	}

	public void setFadeOutTime(double fadeOutTime) {
		this.fadeOutTime = fadeOutTime;
	}
	
	public boolean isKeepAspect() {
		return keepAspect;
	}
	public void setKeepAspect(boolean keepAspect) {
		this.keepAspect = keepAspect;
	}
	
	public double getLastTime() {
		if(duration!=DougaSystem.FOREEVER){
			return startTime+duration;
		}
		return 0;
	}

}
